/*

This program makes FIGURE 2 (the export and import bin figures)

Set the directories here to follow the internal RDC programs
*/



*1.  import table
import excel "$input\afft_restat_01.xlsx", ///
	sheet("FIGURE_2_data") firstrow clear
	

*2. very collapsed bins
gen count=country_count_bin_numeric
	replace count=5 if count==6
	replace count=2 if count==3
collapse (sum) DOMexports FORexports MNEexports DOMimports FORimports MNEimports, by(count)
	
capture label drop bins	
label define bins 1 "2-5" 2 "6-15" 4 "16-25" 5 "26-45" 7 "46+"
label values count bins	
	
*3. Make figures 	
graph bar (asis) DOMexports FORexports MNEexports, over(count) stack ///
	bar(1, fcolor(gs80) lcolor(gs50) fintensity(inten30) lpattern(solid)) ///
    bar(2, fcolor(orange) lcolor(orange) fintensity(inten60) lpattern(dash)) ///
	bar(3, fcolor(orange%99) fintensity(inten90) lcolor(orange) lpattern(solid)) ///
	ytitle(Billions (USD)) title("Exports by Number of Destination Countries") ///	
	legend(order(1 "Domestic" 2 "Foreign MNE" 3 "US MNE") rows(1) region(lcolor(none) lwidth(none))) ///
	graphregion(color(white))
graph export $figs/FIGURE2_exports.png, replace  //  This is FIGURE 2 -  RHS PANEL
	
	
graph bar (asis) DOMimports FORimports MNEimports, over(count) stack ///
	bar(1, fcolor(gs80) lcolor(gs50) fintensity(inten30) lpattern(solid)) ///
    bar(2, fcolor(navy) lcolor(navy) fintensity(inten60) lpattern(dash)) ///
	bar(3, fcolor(blue%99) fintensity(inten90) lcolor(blue) lpattern(solid)) ///
	ytitle(Billions (USD)) title("Imports by Number of Source Countries") ///	
	legend(order(1 "Domestic" 2 "Foreign MNE" 3 "US MNE") rows(1) region(lcolor(none) lwidth(none))) ///
	graphregion(color(white))	
graph export $figs/FIGURE2_imports.png, replace	 //  This is FIGURE 2 -  LHS PANEL


*4. Shares of trade by bins 
foreach vv in exports imports {
	gen tot_`vv'_bin=DOM`vv'+FOR`vv'+MNE`vv'
	egen tot_`vv'=sum(tot_`vv'_bin)
	gen sh_bin_`vv'=tot_`vv'_bin/tot_`vv'
	}
	
browse count sh_bin_exports sh_bin_imports

*share of MNE trade in 26+ bin
import excel "$input\afft_restat_01.xlsx", ///
	sheet("FIGURE_2_data") firstrow clear

gen count=country_count_bin_numeric
	replace count=5 if count==6
	replace count=2 if count==3
	replace count=5 if count==7
collapse (sum) DOMexports FORexports MNEexports DOMimports FORimports MNEimports, by(count)

*Shares of trade by bins
foreach vv in exports imports {
	gen tot_`vv'_bin=DOM`vv'+FOR`vv'+MNE`vv'
	egen tot_`vv'=sum(tot_`vv'_bin)
	gen sh_bin_`vv'=tot_`vv'_bin/tot_`vv'
	gen sh_mne_`vv'=(FOR`vv'+MNE`vv')/tot_`vv'_bin
	}
	
capture label drop bins	
label define bins 1 "2-5" 2 "6-15" 4 "16-25" 5 "26+"
label values count bins		

format %9.2fc sh_bin_exports sh_bin_imports
browse count sh_bin_exports sh_bin_imports  // percent of trade by firms that trade with 26+ countries

// Numbers for text in Section 3.1: note that multi-country importers (exporters) account for 99% of imports (exports)
browse sh_bin_imports if count==5  // Share of multi-country importers' imports by firms that import from more than 25 countries 
browse sh_bin_exports if count==5  // Share of multi-country exporters' exports by firms that export from more than 25 countries 

format %9.2fc sh_mne_exports sh_mne_imports
browse count sh_mne_exports sh_mne_imports  // percent of bins by MNEs

	
capture label drop bins	
label define bins 1 "2-5" 2 "6-15" 3 "11-15" 4 "15-25" 5 "26-45" 6 "36-45" 7 "46+"
label values count bins


*Confirm that we match the aggregate MNE shares in Table 2. (We do!)
  *note that MNEs without foreign manuf affiliates are called Dom Firms in these figures
collapse (sum) DOMexports FORexports MNEexports DOMimports FORimports MNEimports

foreach vv in exports imports {
	gen tot_`vv'=DOM`vv'+FOR`vv'+MNE`vv'
	gen UMNE_sh_`vv'=MNE`vv'/tot_`vv'
	}
	
	


